package com.dhl.command;

/**
 * @title: RemoteController
 * @Author danghailiang
 * @Date: 2022/8/20 12:07
 * @Version 1.0
 */
public class RemoteController {
    /**
     * 打开得命令数组
     */
    private Command[] onCommands;
    /**
     * 关闭命名得数组
     */
    private Command[] offCommands;
    /**
     * 用户保存撤销按钮
     */
    private Command undoCommand;

    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        // 初始化空命令
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    // 设置按钮，保存命令
    public void setCommand(int no, Command onCommand, Command offCommand) {
        this.onCommands[no] = onCommand;
        this.offCommands[no] = offCommand;
    }

    /**
     * 按下按钮
     *
     * @param no 指定按钮
     */
    public void onButtonWasPushed(int no) {
        // 找到按钮，调用相关 开 方法
        this.onCommands[no].execute();
        // 记住此操作用于撤销
        undoCommand = this.onCommands[no];
    }

    public void offButtonWasPushed(int no) {
        // 执行 关 的操作
        this.offCommands[no].execute();
        // 撤销
        undoCommand = this.offCommands[no];
    }
    // 撤销
    public void setUndoCommand(){
        this.undoCommand.undo();
    }
}
